** This dofile makes the results related to summary statistics

** This dofile makes the followings: 
/*
Table 1: Summary Statistics for the HS Six-Digit Product-Level Matching and Firm-Level Matching in Textile/Apparel Trade from Mexico to the United States

Table A4: Summary Statistics

Table A5: Summary Statistics for Product-Level Matching: Regression Samples

Table A6: Summary Statistics for Firm-Level Matching: Regression Samples

Table A9: Product-Level Characteristics in 2004

Table A10: Importer-Product Level Characteristics in 2004

Table A11: Exporter-Product Level Characteristics in 2004
*/

capture ssc install tabform

** For Table 1 (Product level)
use "${output_data}summarystats_product_04050607.dta", clear
preserve
keep if year==2004 | year==2007
xpose, clear varname
order _varname
save "${output}summary_statistics/summarystats0407_productlevel.dta", replace
restore
** For Table A4 (Product level)
preserve
keep if year==2005 | year==2006
xpose, clear varname
order _varname
save "${output}summary_statistics/summarystats0506_productlevel.dta", replace
restore
** For Table 1 (Firm level)
use "${output_data}summarystats_firm_04050607.dta", clear
preserve
keep if year==2004 | year==2007
xpose, clear varname
order _varname
save "${output}summary_statistics/summarystats0407_firmlevel.dta", replace
restore
** For Table A4 (Firm level)
preserve
keep if year==2005 | year==2006
xpose, clear varname
order _varname
save "${output}summary_statistics/summarystats0506_firmlevel.dta", replace
restore


** For Table A5 (Regression sample, Product level)
use "${output_data}summarystats_regsample_product_04050607.dta", clear
xpose, clear varname
order _varname
save "${output}summary_statistics/summarystats_regsample_product_04050607.dta", replace
** For Table A6 (Regression sample, Firm level)
use "${output_data}summarystats_regsample_firm_04050607.dta", clear
xpose, clear varname
order _varname
save "${output}summary_statistics/summarystats_regsample_firm_04050607.dta", replace


** For Table A9
use "${output_data}product_level_characteristics.dta", clear
gen All=1
gen Control=1-Binding
collapse (sum) Control All
save "${output}summary_statistics/product_level_characteristics_obs.dta", replace

use "${output_data}product_level_characteristics.dta", clear
#delimit;
tabform nexp2004 nimp2004 nimp_per_nexp lvalue men woman Wool cotton manmade 
using "${output}summary_statistics/product_level_characteristics_controls.csv", by(Binding) sd vertical sdbracket mtest;
#delimit cr;

eststo clear
eststo: xi: reg nexp2004 Binding, cluster(hs6)
eststo: xi: reg nexp2004 Binding i.hs2, cluster(hs6)
eststo: xi: reg nimp2004 Binding, cluster(hs6)
eststo: xi: reg nimp2004 Binding i.hs2, cluster(hs6)
eststo: xi: reg nimp_per_nexp Binding, cluster(hs6)
eststo: xi: reg nimp_per_nexp Binding i.hs2, cluster(hs6)
eststo: xi: reg lvalue Binding, cluster(hs6)
eststo: xi: reg lvalue Binding i.hs2, cluster(hs6)
eststo: xi: reg men Binding, cluster(hs6)
eststo: xi: reg men Binding i.hs2, cluster(hs6)
eststo: xi: reg woman Binding, cluster(hs6)
eststo: xi: reg woman Binding i.hs2, cluster(hs6)
eststo: xi: reg Wool Binding, cluster(hs6)
eststo: xi: reg Wool Binding i.hs2, cluster(hs6)
eststo: xi: reg cotton Binding, cluster(hs6)
eststo: xi: reg cotton Binding i.hs2, cluster(hs6)
eststo: xi: reg manmade Binding, cluster(hs6)
eststo: xi: reg manmade Binding i.hs2, cluster(hs6)

#delimit;
esttab using "${output}summary_statistics/product_level_characteristics_diff.tex", replace nonumbers indicate("Sector FE (HS2)=_Ihs2*") nocons
 starlevels(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(%9.0f) labels(Obs.)) 
 posthead( &(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)&(9)&(10)&(11)&(12)&(13)&(14)&(15)&(16)&(17)&(18)\\ 
 & \multicolumn{2}{c}{N Exporters}&\multicolumn{2}{c}{N Importers}&\multicolumn{2}{c}{N Importers/N Exporters}&\multicolumn{2}{c}{LnTotalTrade}&\multicolumn{2}{c}{Men}&\multicolumn{2}{c}{Women}&\multicolumn{2}{c}{Wool}&\multicolumn{2}{c}{Cotton}&\multicolumn{2}{c}{Man-Made}  \\
 \hline) nomtitles cells(b(star fmt(3)) se(par fmt(3)))
 substitute((d));
#delimit cr;
 

** For Table A10
use "${output_data}import_product_level_characteristics.dta", clear
collapse (sum) d***, by(Binding)
label var dmaquilashare "Value share of maquila"
label var dnorth "Northern State Dummies"
label var dlfirmhs6import "Log Firm-HS6 Import"
label var dnseller "Number of Sellers"
label var dmainshare "Value share of main partner"
label var dintermed_only "Intermediary Only"
save "${output}summary_statistics/import_product_level_characteristics_obs.dta", replace

use "${output_data}import_product_level_characteristics.dta", clear

#delimit;
tabform intermed_only lfirmhs6import nseller maquilashare mainshare north
using "${output}summary_statistics/import_product_level_characteristics_control.csv", by(Binding) sd vertical sdbracket mtest;
#delimit cr;

eststo clear
eststo: reg intermed Binding
eststo: xi: reg intermed Binding i.hs2
eststo: reg lfirmhs6import Binding
eststo: xi: reg lfirmhs6import Binding i.hs2 
eststo: reg nseller Binding
eststo: xi: reg nseller Binding i.hs2 
eststo: reg maquilashare Binding 
eststo: xi: reg maquilashare Binding i.hs2
eststo: reg mainshare Binding if nseller>1
eststo: xi: reg mainshare Binding i.hs2 if nseller>1
eststo: reg north Binding
eststo: xi: reg north Binding i.hs2

#delimit ;
esttab using "${output}summary_statistics/import_product_level_characteristics_diff.tex", replace nocons nonumbers indicate("Sector FE (HS2)=_Ihs2*")
 starlevels(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(%9.0f) labels(Obs.))
 posthead( &(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)&(9)&(10)&(11)&(12)\\ 
 & \multicolumn{2}{c}{US Intermediary}&\multicolumn{2}{c}{LnTrade}&\multicolumn{2}{c}{N of Partners}&\multicolumn{2}{c}{Main Partner Share}&\multicolumn{2}{c}{Maquiladora}&\multicolumn{2}{c}{Northern State}  \\
 \hline) nomtitles cells(b(star fmt(3)) se(par fmt(3)))
 substitute((d)) label;
#delimit cr;


** For Table A11
use "${output_data}export_product_level_characteristics.dta", clear
collapse (sum) d***, by(Binding)
label var dmaquilashare "Value share of maquila"
label var dnorth "Northern State Dummies"
label var dlfirmhs6export "Log Firm-HS6 Export"
label var dnbuyer "Number of Buyers"
label var dmainshare "Value share of main partner"
label var dintermed_only "Intermediary Only"
save "${output}summary_statistics/export_product_level_characteristics_obs.dta", replace

use "${output_data}export_product_level_characteristics.dta", clear
#delimit;
tabform maquilashare north lfirmhs6export nbuyer mainshare intermed_only
using "${output}summary_statistics/export_product_level_characteristics_control.csv", by(Binding) sd vertical sdbracket mtest;
#delimit cr;

eststo clear
eststo: reg maquilashare Binding
eststo: xi: reg maquilashare Binding i.hs2
eststo: reg north Binding
eststo: xi: reg north Binding i.hs2
eststo: reg lfirmhs6export Binding
eststo: xi: reg lfirmhs6export Binding i.hs2
eststo: reg nbuyer Binding
eststo: xi: reg nbuyer Binding i.hs2
eststo: reg mainshare Binding
eststo: xi: reg mainshare Binding i.hs2
eststo: reg intermed Binding
eststo: xi: reg intermed Binding i.hs2

#delimit ;
esttab using "${output}summary_statistics/export_product_level_characteristics_diff.tex", replace nocons nonumbers indicate("Sector FE (HS2)=_Ihs2*")
 starlevels(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(%9.0f) labels(Obs.)) nocons posthead( &(1)&(2)&(3)&(4)&(5)&(6)&(7)&(8)&(9)&(10)&(11)&(12)\\ 
 & \multicolumn{2}{c}{Maquiladora}&\multicolumn{2}{c}{Northern State}&\multicolumn{2}{c}{LnTrade}&\multicolumn{2}{c}{N of Partners}&\multicolumn{2}{c}{Main Partner Share}&\multicolumn{2}{c}{US Intermediary}  \\
 \hline) nomtitles cells(b(star fmt(3)) se(par fmt(3)))
 substitute((d)) label;
#delimit cr;


